package com.huobi.pro.trade.client;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huobi.HuobiProperty;
import com.huobi.pro.trade.utils.OrderHttpClient;

@Service
public class OrderRestClient {

	@Autowired HuobiProperty huobiProperty;
	
	/**
	 * 
	 * @param accountid
	 * @param symbol
	 * string	true	NA	交易对,即btcusdt, ethbtc...（取值参考GET /v1/common/symbols）
	 * @param type
	 * type	string	true	NA	订单类型，包括buy-market, sell-market, buy-limit, sell-limit, buy-ioc, sell-ioc, buy-limit-maker, sell-limit-maker（说明见下文）, buy-stop-limit, sell-stop-limit, buy-limit-fok, sell-limit-fok, buy-stop-limit-fok, sell-stop-limit-fok
	 * @param amount
	 * @return
	 */
	public String place(String orderid,String accountid,String symbol,String type,String amount,String price) {
		Map<String, String> params = new HashMap<>();
		//取值参考 GET /v1/account/accounts。现货交易使用 ‘spot’ 账户的 account-id；逐仓杠杆交易，请使用 ‘margin’ 账户的 account-id；全仓杠杆交易，请使用 ‘super-margin’ 账户的 account-id
		params.put("account-id", accountid);
		//symbol	string	true	NA	交易对,即btcusdt, ethbtc...（取值参考GET /v1/common/symbols）
		params.put("symbol", symbol);
		//type	string	true	NA	订单类型，包括buy-market, sell-market, buy-limit, sell-limit, buy-ioc, sell-ioc, buy-limit-maker, sell-limit-maker（说明见下文）, buy-stop-limit, sell-stop-limit, buy-limit-fok, sell-limit-fok, buy-stop-limit-fok, sell-stop-limit-fok
		params.put("type", type);
		params.put("amount", amount);
		params.put("price", price);
		//现货交易填写“spot-api”，逐仓杠杆交易填写“margin-api”，全仓杠杆交易填写“super-margin-api”, C2C杠杆交易填写"c2c-margin-api"
		params.put("source","spot-api");
		params.put("client-order-id",orderid);
		params.put("order-id",orderid);

		String res = OrderHttpClient.getInstance().call(huobiProperty.getAccessKey(), huobiProperty.getSecretKey(), "POST", huobiProperty.getOrderUrl() + "/v1/order/orders/place",
				params, new HashMap<>());
		return res;
	}

	public String accounts() {
		Map<String, String> params = new HashMap<>();
		String url ="/v1/account/accounts";
		String res = OrderHttpClient.getInstance().call(huobiProperty.getAccessKey(), huobiProperty.getSecretKey(), "GET", huobiProperty.getOrderUrl() + url,
				params, new HashMap<>());
		return res;
	}
	
	public String cancelOrder(String orderid) {
		String url = "/v1/order/orders/"+orderid+"/submitcancel";
		Map<String, String> params = new HashMap<>();
		String res = OrderHttpClient.getInstance().call(huobiProperty.getAccessKey(), huobiProperty.getSecretKey(), "POST", huobiProperty.getOrderUrl() + url,
				params, new HashMap<>());
		return res;
	}
	
	public String order(String orderid) {
		String url = "/v1/order/orders/"+orderid;
		Map<String, String> params = new HashMap<>();
		String res = OrderHttpClient.getInstance().call(huobiProperty.getAccessKey(), huobiProperty.getSecretKey(), "GET", huobiProperty.getOrderUrl() + url,
				params, new HashMap<>());
		return res;
	}
	
	

	public String queryBalance(String accountId) {
		String url = "/v1/account/accounts/"+accountId+"/balance";
		Map<String, String> params = new HashMap<>();
		String res = OrderHttpClient.getInstance().call(huobiProperty.getAccessKey(), huobiProperty.getSecretKey(), "GET", huobiProperty.getOrderUrl() + url,
				params, new HashMap<>());
		return res;
	}
	
	
}
